﻿using System;
using System.Collections.Generic;
using NGinnBPM.Lib.Data;
using System.Text;
using NGinnBPM.Runtime.Events;
using NGinnBPM.Lib.Schema;
using NGinnBPM.Lib.Exceptions;
using System.Runtime.Serialization;

namespace NGinnBPM.Runtime.Tasks
{
    [Serializable]
    [DataContract]
    public class SendMessageTaskInstance : AtomicTaskInstance
    {
        private string _messageCorrelationId;
        private string _destination = null;

        [DataMember]
        [TaskParameter(IsInput = true, Required = true, DynamicAllowed = true)]
        public string MessageCorrelationId
        {
            get { return _messageCorrelationId; }
            set { _messageCorrelationId = value; }
        }

        [DataMember]
        [TaskParameter(IsInput = true, Required = false, DynamicAllowed = true)]
        public string Destination
        {
            get { return _destination; }
            set { _destination = value; }
        }


        public override void Enable(Dictionary<string, object> inputData)
        {
            base.Enable(inputData);
            MessageToReceiveTask sm = new MessageToReceiveTask();
            sm.MessageCorrelationId = this.MessageCorrelationId;
            sm.MessageBody = new Dictionary<string, object>(this.TaskData);
            if (Destination == null || Destination.Length == 0)
                Context.MessageBus.Notify(sm);
            else
                Context.MessageBus.Send(Destination, sm);
            Complete(null, null);
            //DefaultHandleTaskFinished(GetOutputData());
        }
    }
}
